/*
 * Fifo_queue.c
 *
 *  Created on: Apr 8, 2014
 *      Author: Manuel Sandoval
 */


#include "Fifo_queue.h"



boolean init_queue(queue *q)
{

  q->first = 0;
  q->last = QUEUESIZE-1;
  q->count = 0;

  return true;
}

boolean clear_queue(queue *q)
{
	if(q != NULL_PTR)
	{
		q->first = 0;
		q->last = QUEUESIZE-1;
		q->count = 0;

		return true;
	}
	else
	{
		/* do nothing */
		return false;
	}
}

boolean enqueue(queue *q, Os_TaskControlBlockType * Task)
{

	if(q == NULL_PTR)
	{
		return false;
	}
	else
	{
		/* do nothing */
	}

	if (q->count >= QUEUESIZE)
	{
		return false;
	}
	else
	{
		q->last = (q->last+1) % QUEUESIZE;
		q->q[ q->last ] = (*Task);
		q->count = q->count + 1;

		return true;
	}
}


boolean dequeue(queue *q, Os_TaskControlBlockType * Task)
{

	if (q->count <= 0)
	{
		return false;
	}
	else
	{
		(*Task) = q->q[ q->first ];
		q->first = (q->first+1) % QUEUESIZE;
		q->count = q->count - 1;
	}

	return true;
}


boolean IsEmpty(queue *q)
{
	if (q->count <= 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

